namespace AtomicCms.Tests.Controllers
{
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using Common.Entities;
    using Moq;
    using NUnit.Framework;
    using Services.Abstract;
    using Web.Areas.Admin.Controllers;

    [TestFixture]
    public class CmsPageControllerBehavior
    {
        #region Setup/Teardown

        [SetUp]
        public void SetUp()
        {
            this.pageService = new Mock<IPageService>();
            this.controller = new CmsPageController(this.pageService.Object);
            var routeData = new RouteData();
            var httpContext = new Mock<HttpContextBase>();

            ControllerContext controllerContext = new
                ControllerContext(httpContext.Object,
                                  routeData,
                                  this.controller);

            this.controller.ControllerContext = controllerContext;
        }

        #endregion

        private Mock<IPageService> pageService;
        private CmsPageController controller;

        [Test]
        public void Should_Save_Tags_For_New_Page()
        {
            CmsPage pageFromDb = new CmsPage {Id = 3, PageTitle = "elloo", Alias = "old alias"};
            this.pageService.Setup(x => x.Load(3)).Returns(pageFromDb);
            this.pageService.Setup(x => x.Save(pageFromDb, "alex")).Callback(
                () =>
                    {
                        pageFromDb.Tags.Add(new CmsTag {Name = "ASP.NET"});
                        pageFromDb.Tags.Add(new CmsTag {Name = "C#"});
                        pageFromDb.Tags.Add(new CmsTag {Name = "CSS"});
                    });

            FormCollection forms = new FormCollection();
            forms.Add("Tags", "ASP.NET, C#, CSS");

            this.controller.Edit(3, forms, "alex");

            Assert.IsNotEmpty((ICollection) pageFromDb.Tags);
            Assert.AreEqual(3, pageFromDb.Tags.Count);
        }

        [Test]
        public void Should_Update_Tags_For_Existing_Pages()
        {
            ICollection<CmsTag> tags = new List<CmsTag> {new CmsTag {Name = "Asp.NET"}};
            CmsPage pageFromDb = new CmsPage {Id = 3, PageTitle = "elloo", Alias = "old alias", Tags = tags};
            this.pageService.Setup(x => x.Load(3)).Returns(pageFromDb);
            this.pageService.Setup(x => x.Save(pageFromDb, "alex")).Callback(
                () =>
                    {
                        pageFromDb.Tags.Clear();
                        pageFromDb.Tags.Add(new CmsTag { Name = "ASP.NET" });
                        pageFromDb.Tags.Add(new CmsTag { Name = "C#" });
                        pageFromDb.Tags.Add(new CmsTag { Name = "Java" });
                    });
            FormCollection forms = new FormCollection();
            forms.Add("Tags", "ASP.NET, C#, Java");

            this.controller.Edit(3, forms, "alex");

            Assert.IsNotEmpty((ICollection) pageFromDb.Tags);
            Assert.AreEqual(3, pageFromDb.Tags.Count);
            Assert.AreEqual("ASP.NET", pageFromDb.Tags.First().Name);
            Assert.AreEqual("Java", pageFromDb.Tags.Last().Name);
        }
    }
}